package force;

import backbone.IForceable;
import backbone.IUpdatable;
import java.util.ArrayList;
import java.util.List;
import main.Simulation;


/**
 * Govern environmental effects that influence all forceable
 * simulation objects.
 * 
 * @author Paul
 * @author Lance
 */
public abstract class GlobalForce implements IUpdatable {

    private List<IForceable> myForceables;
    private double myInitialMagnitude;
    private double myMagnitude;
    private String myName;

    /**
     * 
     * @param magnitude Magnitude of the force
     * @param name Name of the force.
     */
    public GlobalForce(double magnitude, String name) {
        myForceables = new ArrayList<IForceable>();
        myMagnitude = magnitude;
        myInitialMagnitude = magnitude;
        myName = name;
    }

    /**
     * 
     * @param magnitude
     */
    public void setMagnitude(double magnitude) {
        myMagnitude = magnitude;
    }

    /**
     * 
     * @return
     */
    public double getMagnitude() {
        return myMagnitude;
    }

    /**
     * 
     * @return
     */
    public List<IForceable> getForceables() {
        return myForceables;
    }

    /**
     * 
     * @return
     */
    public String getName() {
        return myName;
    }

    /**
     * 
     * @param forceable
     */
    public void add(IForceable forceable) {
        myForceables.add(forceable);
    }

    /**
     * Removes the specified object from the class'
     * list of global forces.
     * 
     * @param forceable
     */
    public void remove(IForceable forceable) {
        for (int i = myForceables.size() - 1; i <= 0; i--) {
            if (myForceables.get(i).equals(forceable)) {
                myForceables.remove(i);
            }
        }
    }

    /**
     * If the magnitude of the force isn't equal to zero,
     * sets it to zero. Otherwise resets it to its original
     * magnitude.
     */
    public void toggle() {
        if (myMagnitude == myInitialMagnitude) {
            setMagnitude(0);
        }
        else {
            setMagnitude(myInitialMagnitude);
        }
    }

    /**
     * @param canvas The Simulation which I want to update.
     * @param dt The time interval between updates.
     */
    public abstract void update(Simulation canvas, double dt);
}
